package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.text.TextUtils;
import android.widget.EditText;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 修改个人简介
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/23
 */

public class EditSelfIntroductionActivity extends BaseChatActivity implements ICommonViewUi {


    @InjectView(R.id.introductionEdt)
    EditText introductionEdt;

    private ICommonRequestPresenter iCommonRequestPresenter;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_edit_self_introduction;
    }

    UserInfo userInfo;
    @Override
    protected void initViewsAndEvents() {
        setTitle("Self-Introduction");
        setRightTitle("Confirm");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        String data= PreferenceUtils.getPrefString(this, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            userInfo = jsonHelper.getData(data, "result");
            bindData();
        }

    }

    private void bindData(){
        introductionEdt.setText(userInfo.getTextDescription());
    }


    @OnClick(R.id.toolbar_right_title)
    public void onRightClick(){
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
    }

    @Override
    public void toRequest(int eventTag) {
        if (userInfo == null||TextUtils.isEmpty(introductionEdt.getText().toString().trim())) {
            return;
        }
        RequestParams params = new RequestParams();
        params.put("accountName", userInfo.getAccountName());
        params.put("textDescription", introductionEdt.getText().toString().trim());
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.UPDATE_INFO, params);
    }
    @Override
    public void getRequestData(int eventTag, String result) {
//        showToastLong(HttpStatusUtil.getStatusMsg(result));
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.UPDATE_INFO);
        EventBus.getDefault().post(postResult);
        finish();
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }
    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if(status){
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        }else{
            mPostingdialog.dismiss();
        }
    }

}
